package com.z.platform.controller.persistence.orm;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.z.platform.controller.persistence.orm.annotation.Column;
import com.z.platform.controller.persistence.orm.annotation.Id;
import com.z.platform.controller.persistence.orm.annotation.Table;
import com.z.platform.controller.persistence.orm.metadataModel.ColumnMetadata;
import com.z.platform.controller.persistence.orm.metadataModel.Entity;
import com.z.platform.controller.persistence.orm.metadataModel.TableMetadata;
import com.z.ssmdemo.log.factory.ELogger;
import com.z.ssmdemo.log.factory.LogFactory;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

/**
 * desc TableAnalyzer记录与数据库对应的不可变的数据。不在需要
 * @author haonan.zhang
 */
public class TableAnalyzer {
    private static final ELogger LOG = LogFactory.getLog(TableAnalyzer.class);
    public static Map<Class<?>, TableMetadata> tableMaps = Maps.newConcurrentMap();

    public static TableMetadata parseTable(Entity entity) {

        Class<? extends Entity> entityClass = entity.getClass();
        if (Objects.nonNull(tableMaps.get(entityClass))) {
            return tableMaps.get(entityClass);
        }
        TableMetadata tableMetadata = new TableMetadata();
        Table table = entityClass.getAnnotation(Table.class);
        String tableName = table.tableName();
        tableMetadata.setTableName(tableName);
        if (Strings.isNullOrEmpty(tableName)) {
            throw new RuntimeException("表名为空");
        }
        LOG.debug("开始将实体中的配置封装到TableMetadata中");
        //先过滤不为空的 在进行记录
        Arrays.stream(entityClass.getDeclaredFields()).filter(itemField -> {
            Id id = itemField.getAnnotation(Id.class);
            if (Objects.nonNull(id)) {
                return !Strings.isNullOrEmpty(id.priMaryKey());
            }
            Column fieldAnnotation = itemField.getAnnotation(Column.class);
            if (Objects.nonNull(fieldAnnotation)) {
                return !Strings.isNullOrEmpty(fieldAnnotation.column());
            }
            return false;
        }).forEach(notNullItemField -> {
            try {
                Object columnValue = notNullItemField.get(entity);
                Id id = notNullItemField.getAnnotation(Id.class);
                Column notNullItemFieldAnnotation = notNullItemField.getAnnotation(Column.class);
                ColumnMetadata columnMetadata = new ColumnMetadata();
                //封装主键数据
                if (Objects.nonNull(id) && !Strings.isNullOrEmpty(id.priMaryKey())) {
                    //设置主键类型
                    columnMetadata.setColumnType(notNullItemField.getType());
                    //数值对应主键的数据库列名
                    columnMetadata.setColumnName(id.priMaryKey());
                    //if (Objects.nonNull(columnValue)) {
                    //    columnMetadata.setColumnValue(columnValue.toString());
                    //}
                    columnMetadata.setFieldName(notNullItemField.getName());
                    tableMetadata.setPrimaryKey(columnMetadata);
                } else if (Objects.nonNull(columnValue) && Objects.nonNull(notNullItemFieldAnnotation)) {
                    columnMetadata.setColumnType(notNullItemField.getType());
                    //设置和数据库对应的列名
                    columnMetadata.setColumnName(notNullItemFieldAnnotation.column());
                    //设置列对应的值（这一步放到SqlAnalyzer中去做，实现可变数据与不可变数据解耦，将不可变数据使用享元模式进行存储，每次使用从容器中获取）
                    //columnMetadata.setColumnValue(columnValue.toString());
                    //记录类中的属性
                    columnMetadata.setFieldName(notNullItemField.getName());
                    tableMetadata.setColumnMetadata(columnMetadata);
                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
        LOG.debug("将实体中的配置封装到TableMetadata中");
        //封装对应的插入语句
        tableMaps.put(entityClass, tableMetadata);
        return tableMetadata;
    }


}
